package com.reebake.ideal.jdbc;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class FlywayEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
    private final static String FLYWAY_ENABLED_KEY = "spring.flyway.enabled";

    public int getOrder() {
        return -2147483638;
    }

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String enabled = environment.getProperty(FLYWAY_ENABLED_KEY);
        if(StrUtil.isNotBlank(enabled)) {
            return;
        }
        log.info("set flyway.enabled to false, please set true when use flyway default feature.");
        Map<String, Object> configMap = new HashMap<>();
        configMap.put(FLYWAY_ENABLED_KEY, "false");

        MapPropertySource propertySource = new MapPropertySource("flyway", configMap);
        environment.getPropertySources().addFirst(propertySource);
    }
}
